package de.openkanban.client.ui.board;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragAppearance;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

import de.openkanban.client.ui.presenter.PreparedRequest;
import de.openkanban.client.ui.presenter.ToggleKanbanRequestCallback;
import de.openkanban.client.util.RootPanel;
import de.openkanban.shared.dto.KanbanDto;

/**
 * GUI Element for the KanbanView
 */
public class KanbanView extends Canvas {

	private Label titleLabel;

	private VLayout mainLayout;

	private IButton pullableButton;

	private Long parentActivityId;
	private KanbanDto kanbanDto;
	private ToggleKanbanRequestCallback toggleKanbanRequestCallback;
	private HLayout kanbanHeaderHLayout;
	private HLayout kanbanFooterHLayout;
	private Label effortLabel;
	private Label effortNameLabel;

	public KanbanView(Long parentActivityId) {
		this.parentActivityId = parentActivityId;

		toggleKanbanRequestCallback = new ToggleKanbanRequestCallback(this);

		pullableButton = new IButton();
		pullableButton.setWidth(40);
		pullableButton.setHeight(10);
		pullableButton.setSrc(Page.getAppImgDir() + "kanban/pullok.png");
		pullableButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				kanbanDto.setPullable(!kanbanDto.getPullable());

				PreparedRequest prepReq = new PreparedRequest("kanban", "update", kanbanDto);
				prepReq.sendAsyncRequest(toggleKanbanRequestCallback);

			}
		});

		titleLabel = new Label();
		titleLabel.setAutoHeight();
		titleLabel.setWidth100();
		titleLabel.setAlign(Alignment.LEFT);
		titleLabel.setValign(VerticalAlignment.TOP);
		titleLabel.setMargin(2);
		titleLabel.setStyleName("kanbanTitle");

		effortNameLabel = new Label("Aufwand: ");
		effortNameLabel.setAutoWidth();
		effortNameLabel.setAutoHeight();
		effortNameLabel.setAlign(Alignment.RIGHT);
		effortNameLabel.setStyleName("kanbanEffortName");

		effortLabel = new Label();
		effortLabel.setAutoWidth();
		effortLabel.setAutoHeight();
		effortLabel.setAlign(Alignment.RIGHT);
		effortLabel.setValign(VerticalAlignment.TOP);
		effortLabel.setStyleName("kanbanEffort");

		Layout headerPlaceholderHLayout = new Layout();
		headerPlaceholderHLayout.setWidth100();
		Layout footerPlaceholderHLayout = new Layout();
		footerPlaceholderHLayout.setWidth100();

		kanbanHeaderHLayout = new HLayout();
		kanbanHeaderHLayout.addMember(headerPlaceholderHLayout);
		kanbanHeaderHLayout.addMember(pullableButton);

		kanbanFooterHLayout = new HLayout();
		kanbanFooterHLayout.addMember(footerPlaceholderHLayout);
		kanbanFooterHLayout.addMember(effortNameLabel);
		kanbanFooterHLayout.addMember(effortLabel);

		mainLayout = new VLayout();
		mainLayout.setAutoHeight();
		mainLayout.setWidth100();

		mainLayout.addMember(kanbanHeaderHLayout);
		mainLayout.addMember(titleLabel);
		mainLayout.addMember(kanbanFooterHLayout);

		// kanban view properties
		setAutoHeight();

		setBackgroundColor("#F8F8F8");
		setStyleName("kanban");
		setShowEdges(true);
		setEdgeShowCenter(false);
		setEdgeImage("corners/kanbanShadow.png");
		setEdgeSize(10);
		setEdgeOffset(10);
		setShowDragShadow(true);
		setShadowSoftness(10);
		setDragAppearance(DragAppearance.TARGET);

		setCanDrag(true);
		setCanDragReposition(true);
		setCanDrop(true);
		setShowHover(false);

		addDoubleClickHandler(kanbanViewDoubleClickHandler(this));

		addChild(mainLayout);
	}

	public KanbanView(KanbanDto kanbanDto, Long parentActivityId) {
		this(parentActivityId);
		this.kanbanDto = kanbanDto;

		titleLabel.setContents(kanbanDto.getTitle());
		effortLabel.setContents(kanbanDto.getEffort());
		togglePullableButton();

		setTitle(kanbanDto.getKanbanId().toString());
	}

	private DoubleClickHandler kanbanViewDoubleClickHandler(final KanbanView kanbanView) {
		return new DoubleClickHandler() {
			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				RootPanel.activateEditKanbanMode(true, kanbanView);
			}
		};
	}

	public void togglePullableButton() {
		if (kanbanDto.getPullable()) {
			pullableButton.setSrc(Page.getAppImgDir() + "kanban/pullok.png");
		} else {
			pullableButton.setSrc(Page.getAppImgDir() + "kanban/pullnok.png");
		}
	}

	public Label getTitleLabel() {
		return titleLabel;
	}

	public void setTitleLabel(Label titleLabel) {
		this.titleLabel = titleLabel;
	}

	public KanbanDto getKanbanDto() {
		return kanbanDto;
	}

	public void setKanbanDto(KanbanDto kanbanDto) {
		this.kanbanDto = kanbanDto;
		titleLabel.setContents(kanbanDto.getTitle());
		effortLabel.setContents(kanbanDto.getEffort());
		togglePullableButton();
	}

	public Long getParentActivityId() {
		return parentActivityId;
	}

	public void setParentActivityId(Long parentActivityId) {
		this.parentActivityId = parentActivityId;
	}

	public boolean isReadyForPull() {
		return kanbanDto.getPullable();
	}

}